<?php 
/* This script uses the minifier to provide a single 
 * minified package of either css or javascript files.
 * 
 * That's why it is called min pack. Because it packages
 * the files required into one pack and the minifies them.
 */

// the global configuration file
require_once("config.php");

// external minification classes
require_once("lib/Minify/JSMin.class.php");
require_once("lib/Minify/Minify_HTML.class.php");
require_once("lib/Minify/Minify_Javascript.class.php");

// the internal minification class
require_once("class/minify.class.php");
require_once("class/cache.class.php");
require_once("class/database.class.php");
require_once("class/blog.class.php");

// instanciate the blog
$b = new blog($_SERVER["HTTP_HOST"]);

// define the required java script files
$js_pack = explode(";", CONFIG_MINPACK_JS);
$css_pack = explode(";", CONFIG_MINPACK_CSS);

/* check if the required param exists
 * that defines the type of pack (css or js) */
if(array_key_exists("t", $_GET)==true
	&&array_key_exists("tag", $_GET)==true){
	// get the requested pack type
	$pack_type = $_GET["t"];

	// check if js pack is requested
	if($pack_type=="js"){
		$jsMinpackTag = minify::getMinpackTag(CONFIG_MINPACK_JS);

		// identifies the file name
		$js_minpackCacheFile = "minpack/".$jsMinpackTag.".js";

		// will hold all script code
		$js_content = "";
		
		/* if the requested file already exists on 
			the disk then use the disk cached version 
			instead of re-processing the js minpack file */
		if(file_exists($js_minpackCacheFile)){
			// just pull the content from the local file
			$js_content = file_get_contents($js_minpackCacheFile);
		}else{
			// walk through all js files and pack them
			for($j=0;$j<count($js_pack);$j++){
				// fill the package
				$js_content .= file_get_contents(CONFIG_ROOTDIR.$js_pack[$j])."\n";
			}
		
			// safely minify the javascript
			$js_content = minify::minifyJS($js_content);

			// flush the minpack to the disk for caching
			file_put_contents($js_minpackCacheFile, $js_content);

			// remove any outdated file
			removeOutdatedCacheFile($js_minpackCacheFile);
		}
	
		// set the expires header to enforce caching
		header("Cache-Control: max-age=31556926");
		header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+31556926)));
		
		// set the correct header for this js pack
		header("Content-Type: text/javascript");
		
		// finally output the minpack
		print($js_content);
		
	}// check if js pack is requested
	if($pack_type=="css"){
		$cssMinpackTag = minify::getMinpackTag(CONFIG_MINPACK_CSS);

		// identifies the file name
		$css_minpackCacheFile = "minpack/".$cssMinpackTag.".css";

		// will hold all script code
		$css_content = "";
		
		if(file_exists($css_minpackCacheFile)){
			// get the css content from the cache file
			$css_content = file_get_contents($css_minpackCacheFile);
		}else{
			// walk through all css files and pack them
			for($c=0;$c<count($css_pack);$c++){
				// fill the package
				$css_content .= file_get_contents(CONFIG_ROOTDIR.$css_pack[$c])."\n";
			}
			
			// change the host name for the images in the css file
			// so that they use the available CDN hosts for delivery
			$hostlist = explode(";", CONFIG_CDN_IMG_DOMAIN);
			preg_match_all("|url\('([^>]*)'\);|U", $css_content, $found);

			// when images were found, go through them and add the domain name
			if(count($found)>0){
				$h = 0;
				for($i=0;$i<count($found[0]);$i++){
					if($h>=count($hostlist)){
						$h=0;
					}
					
					$oldVal = $found[0][$i];
					$newVal = "url('http://".$hostlist[$h].$found[1][$i]."');";
					$css_content = str_replace($oldVal, $newVal, $css_content);
					
					$h++;
				}
			}

			// minifiy the css package
			$css_content = minify::minifyCSS($css_content);

			// flush the minpack to the disk for caching
			file_put_contents($css_minpackCacheFile, $css_content);

			// remove any outdated file
			removeOutdatedCacheFile($css_minpackCacheFile);
		}
		
		// set the expires header to enforce caching
		header("Cache-Control: max-age=31556926");
		header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
		header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+31556926)));
		
		// set the correct header for this css pack
		header("Content-Type: text/css");
		
		// finally output the minpack
		print($css_content);
	}
	
}

/* removes an outdated cache file from the minpack folder:
	parameter is the current active file name. This function 
	looks for the file extension and removes all other files
	with that extension that are not the current file */
function removeOutdatedCacheFile($currentFileName){
	$fileExt = substr($currentFileName, strrpos($currentFileName, ".")+1);
	$minpackCacheFile = str_replace("minpack/", "",$currentFileName);

	if($hndl=opendir('minpack/')){
		while (false!==($file=readdir($hndl))) {
			if($file!=".."&&$file!="."&&$file!=$minpackCacheFile){
				$cfileExt = str_replace(".","",substr($file,strrpos($file, ".")));
				// make sure the current file and the existing file ext match
				if($cfileExt == $fileExt){
					unlink("minpack/".$file);
				}
			}
    	}
	}
}
?>